/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apt.epj4.db;

import apt.epj4.bean.Admins;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author NGUYENVANHUNG
 */
public class AdminsDB {

    public static ArrayList<Admins> getAllStudents() {
        ArrayList<Admins> list = null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            list = new ArrayList<Admins>();
            conn = MyConnection.getConnection();
            stmt = conn.prepareStatement("Select * from Admins");
            rs = stmt.executeQuery();

            //load data for Vector
            while (rs.next()) {
                Admins ad = new Admins();
                ad.setAdminID(rs.getInt("AdminID"));
                ad.setAdminName(rs.getString("AdminName"));
                ad.setEmail(rs.getString("Email"));
                ad.setPassword(rs.getString("Password"));
                ad.setPhone(rs.getString("Phone"));
                ad.setUsername(rs.getString("Username"));

                list.add(ad);//add bean to list
            }
        } catch (Exception ex) {
            return null;
        } finally {
            MyConnection.closeConnection(rs, stmt, conn);
        }
        return list;
    }

    //add student
    public static boolean checkLogin(String username, String password) {
        boolean login = false;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = MyConnection.getConnection();
            if (conn != null) {
                //get statement
                String sql = "select * from tblAdmins where Username = ? and Password = ?";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, username);
                stmt.setString(2, password);
                rs = stmt.executeQuery();
                if (rs.next()) {
                    login = true;
                    System.out.println("ket noi thanh cong");
                }
            }
        } catch (Exception ex) {
            return false;
        } finally {
            MyConnection.closeConnection(rs, stmt, conn);
            System.out.println("ket noi khong thanh cong");
        }
        return login;
    }

    //add student
    public static boolean addAdmin(String StudentName, String Username, String Password, String Phone, String Email) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            //get connection
            conn = MyConnection.getConnection();
            if (conn != null) {
                //get statement
                String sql = "Insert into Student(StudentName, Username, Password, Phone, Email) values(?,?,?,?,?)";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, StudentName);
                stmt.setString(2, Username);
                stmt.setString(3, Password);
                stmt.setString(4, Phone);
                stmt.setString(5, Email);
                stmt.execute();
            }
        } catch (Exception ex) {
            return false;
        } finally {
            MyConnection.closeConnection(null, stmt, conn);
        }
        return true;
    }
}
